using Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TGame.ECS;
using TGame.Message;

namespace Koakuma.Game.AuctionUI
{
    public class AuctionComponent:ECSComponent
    {
        public List<AuctonItemInfo> saleItems;
        public Dictionary<int,MyItemInfo> allItems;
        public List <MyBagItemInfo> bagItems;
        public int goldCount;
    }
    public class AuctionAwakeSystem : AwakeSystem<AuctionComponent>
    {
        public override void Awake(AuctionComponent c)
        {
            c.goldCount = 10000;
            c.saleItems= ListPool<AuctonItemInfo>.Obtain();
            c.allItems = DictionaryPool<int, MyItemInfo>.Obtain();
            c.bagItems=ListPool<MyBagItemInfo>.Obtain();
            for (int i = 0; i < MyItemConfig.Count; i++)
            {
                MyItemConfig myItemConfig=MyItemConfig.ByIndex(i);
                MyItemInfo myItem = new MyItemInfo()
                {
                    id = myItemConfig.ID,
                    itemName = myItemConfig.Name,
                    level = myItemConfig.Level,
                    type=myItemConfig.MyItemType,
                    use=myItemConfig.Use,
                    getWay=myItemConfig.GetWay,
                    icon=myItemConfig.Icon
                };
                c.allItems.Add(myItem.id,myItem);
                if(i==0) c.bagItems.Add(new MyBagItemInfo() {itemInfo=myItem,count=10 });
                if(i==1) c.bagItems.Add(new MyBagItemInfo() {itemInfo=myItem,count=20 });
                AuctonItemInfo auctonItemInfo = new AuctonItemInfo() { itemInfo = myItem, count = UnityEngine.Random.Range(1, 10), totalPrice = UnityEngine.Random.Range(100, 1000) };
                auctonItemInfo.price = auctonItemInfo.totalPrice / auctonItemInfo.count;
                c.saleItems.Add(auctonItemInfo);
            }
            for (int i = 0;i < 14; i++)
            {
                c.bagItems.Add(new MyBagItemInfo());
            }
        }
    }
    public class ChangeGoldHandler : MessageHandler<MessageType.GoldChanged>
    {
        public override async Task HandleMessage(MessageType.GoldChanged arg)
        {
            AuctionComponent auction= GameManager.ECS.World.GetComponent<AuctionComponent>();
            auction.goldCount += arg.count;
            await GameManager.Message.Post(new MessageType.GoldCount() { count=auction.goldCount});
        }
    }
    public class AddItemToBagHandler : MessageHandler<MessageType.AddBagItem>
    {
        public override async Task HandleMessage(MessageType.AddBagItem arg)
        {
            AuctionComponent auction = GameManager.ECS.World.GetComponent<AuctionComponent>();
            bool isHas = false;
            for (int i = 0; i < auction.bagItems.Count; i++)
            {
                if (auction.bagItems[i].itemInfo != null && auction.bagItems[i].itemInfo.id==arg.itemInfo.id)
                {
                    auction.bagItems[i].count += arg.count;
                    isHas = true;
                    break;
                }
            }
            if (!isHas)
            {
                for (int i = 0; i < auction.bagItems.Count; i++)
                {
                    if (auction.bagItems[i].itemInfo == null)
                    {
                        auction.bagItems[i].itemInfo = arg.itemInfo;
                        auction.bagItems[i].count = arg.count;
                        break;
                    }
                }
            }
            await GameManager.Message.Post(new MessageType.BagItemChanged());
        }
    }
}
